package com.foreveross.fetl.core.domain.freight.repository.impl;

import java.lang.reflect.Field;
import java.util.List;
import java.util.Set;

import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;

import org.springframework.stereotype.Repository;

import com.foreveross.fetl.common.db.hbase.HBaseCellModel;
import com.foreveross.fetl.common.db.hbase.HBaseRowModel;
import com.foreveross.fetl.common.domain.IBaseEntity;
import com.foreveross.fetl.common.jpa.EntityHBaseRepository;
import com.foreveross.fetl.common.jpa.HBaseJpaException;
import com.foreveross.fetl.common.jpa.annontation.HBaseColumn;
import com.foreveross.fetl.core.domain.freight.hbase.PlaneInfoEntity;
import com.foreveross.fetl.core.domain.freight.repository.IPlaneInfoRepository;
import com.foreveross.fetl.util.convert.ValueConvertFatory;

@Repository("planeInfoRepository")
public class PlaneInfoRepositoryImpl extends EntityHBaseRepository implements IPlaneInfoRepository{
	public String TransitchangeEntityClassType = "java.util.List<com.foreveross.fetl.common.domain.hbase.TransitchangeEntity>";
	public String AgentEntityClassType = "java.util.Set<com.foreveross.fetl.core.domain.freight.hbase.AgentEntity>";
	public String CabinEntityClassType = "java.util.Set<com.foreveross.fetl.core.domain.freight.hbase.CabinEntity>";
	
	public List<PlaneInfoEntity> findByRowkey(String start, String end) {
		return super.findByRowkey(PlaneInfoEntity.class, start, end);
	}

	@SuppressWarnings("hiding")
	@Override
	protected <PlaneInfoEntity extends IBaseEntity> PlaneInfoEntity convert2Entity(Class<PlaneInfoEntity> clazz,
			HBaseRowModel model) {
		if(model == null) return null;
		PlaneInfoEntity entity = null;
		HBaseCellModel cell = null;
		HBaseColumn hca = null;
		OneToMany otm = null;
		Field[] fields = null;
		try {
			entity = clazz.newInstance();
			fields = clazz.getDeclaredFields();
			
			for(Field f : fields){
				//凡是集合对象的，暂不保存
				if(f.getAnnotation(ManyToOne.class) != null ||
						f.getAnnotation(OneToOne.class) != null) continue;
				otm = f.getAnnotation(OneToMany.class);
				
				f.setAccessible(true);
				if(otm != null){
					String tmpGenericType = f.getGenericType().toString();
					Set<HBaseCellModel> tmpCells = model.getCells();
					if(tmpGenericType.equals(TransitchangeEntityClassType)){
						f.set(entity, PlaneInfoHbaseUtils.getTransitchangeEntityList(tmpCells));
					}else if (tmpGenericType.equals(AgentEntityClassType)){
//						f.set(entity, HbaseUtils.getAgent(tmpCells));
					}else if (tmpGenericType.equals(CabinEntityClassType)){
						f.set(entity, PlaneInfoHbaseUtils.getCabinEntitySet(tmpCells));
					}
				}
				hca = f.getAnnotation(HBaseColumn.class);
				if(hca != null){
					//使用columnDefinition与name作为列簇，列名
					cell = model.lookupCell(hca.family(), hca.qualifier());
					if(cell != null){
						f.set(entity, ValueConvertFatory.string2Object(f.getGenericType().toString(), cell.getValue(), hca.format()));
					}
				}
			}
			
			entity.setId(model.getRowkey());
		}catch (Exception e) {
			e.printStackTrace();
			throw new HBaseJpaException(e);
		}finally{
			 cell = null;
			 hca = null;
			 fields = null;
		}
		return entity;
	}
	
	
}
